package com.hwadee.property.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hwadee.property.dto.HousePersonal;
import com.hwadee.property.pojo.House;
import com.hwadee.property.service.IBuildingService;
import com.hwadee.property.service.ICommunityService;
import com.hwadee.property.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class DtoUtil {

    @Autowired
    private ICommunityService communityService;

    @Autowired
    private IHouseService houseService;

    @Autowired
    private IBuildingService buildingService;

    public List<HousePersonal> listByOwnerIdPersonal(Long userId) {
        if (userId == null) {
            throw new BusinessException(ErrorCodes.INVALID_PARAMETER, "业主ID不能为空");
        }
        List<House> houses = houseService.listByOwnerId(userId);
        List<HousePersonal> housePersonals = new ArrayList<>();

        for (House i : houses){
            String houseName = getHouseName(i);
            Long houseId = i.getId();
            String checkInDate = i.getCheckInDate();

            HousePersonal housePersonal = new HousePersonal();
            housePersonal.setHouseId(houseId);
            housePersonal.setHouseName(houseName);
            housePersonal.setCheckInDate(checkInDate);

            housePersonals.add(housePersonal);
        }

        return housePersonals;
    }

    public String getHouseName(House house){
        return communityService.getById(buildingService.getById(house.getBuildingId()).getCommunityId()).getCommunityName() + "小区" +
                buildingService.getById(house.getBuildingId()).getBuildingNumber() + "栋" +
                buildingService.getById(house.getBuildingId()).getBuildingUnit() + "单元" +
                house.getFloor().toString() + "0" + house.getRoomNumber() + "房";
    }
}
